import { ref, computed } from 'vue';
import { defineStore } from 'pinia';
import { type UserEntity } from 'spen-common/src/entity/User';
interface Filter {
  invoke: (keyCode: string, targetValue: string)=>number

}
export const useCounterStore = defineStore('counter', () => {
  const count = ref(0)
  const doubleCount = computed(() => count.value * 2)
  function increment() {
    count.value++
  }
  return { count, doubleCount, increment }
})

export const user = defineStore('user', ()=>{
  const user = ref<UserEntity|null>(null);
  return {
    user,
    isLogin(){
      if(user.value == null && localStorage.getItem("user")){
        return false;
      }
      return true;
    },
    setUser(_user: UserEntity){
      localStorage.setItem("user", JSON.stringify(_user));
      user.value = _user;
    },
  }
})

export const wordFilter = defineStore('inputKey', ()=>{
  const filterList = ref<Map<string, Filter>>(new Map());
  return {
    addFilter(name: string,filter: Filter){
      filterList.value.set(name, filter);
    },
    getFilter(){
      return filterList.value;
    },
    removeFilter(name: string){
      filterList.value.delete(name);
    },
    checkFilter(name: string){
      if(filterList.value.get(name) == null ){
        return false;
      }
      return true;
    }
  }
})

export type {Filter}

export const FilterResult = {
    preventDefault: 0b1,
    exit: 0b10,
    null: 0
}